//
//  OrderCell.m
//  Order
//
//  Created by funny on 12-3-21.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "OrderCell.h"


@implementation OrderCell

@synthesize row, packageCount, singleCount;
@synthesize orderDelegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code.
		
		row = 0;
		packageCount = 0;
		singleCount = 0;
		
		name = [[[UILabel alloc] initWithFrame:CGRectMake(20.0f, 30.0f, self.frame.size.width, 60.0f)] autorelease];
		name.textAlignment = UITextAlignmentCenter;
		name.font = [UIFont systemFontOfSize:50.0f];
		name.backgroundColor = [UIColor clearColor];
		name.text = @"猪肉饭";
		[self addSubview:name];
		
		UIButton *packageBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
		[packageBtn setTitle:@"套餐" forState:UIControlStateNormal];
		packageBtn.titleLabel.font = [UIFont systemFontOfSize:30.0f];
		packageBtn.frame = CGRectMake(500.0f, 10.0f, 100.0f, 100.0f);
		[packageBtn addTarget:self action:@selector(packageAdd:) forControlEvents:UIControlEventTouchUpInside];
		[self addSubview:packageBtn];
		
		packageCountLabel = [[[UILabel alloc] initWithFrame:CGRectMake(600.0f, 30.0f, self.frame.size.width, 60.0f)] autorelease];
		packageCountLabel.font = [UIFont systemFontOfSize:50.0f];
		packageCountLabel.backgroundColor = [UIColor clearColor];
		packageCountLabel.text = [NSString stringWithFormat:@" * %d", packageCount];
		[self addSubview:packageCountLabel];
		
		UIButton *singleBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
		[singleBtn setTitle:@"单点" forState:UIControlStateNormal];
		singleBtn.titleLabel.font = [UIFont systemFontOfSize:30.0f];
		singleBtn.frame = CGRectMake(700.0f, 10.0f, 100.0f, 100.0f);
		[singleBtn addTarget:self action:@selector(singleAdd:) forControlEvents:UIControlEventTouchUpInside];
		[self addSubview:singleBtn];
		
		singleCountLabel = [[[UILabel alloc] initWithFrame:CGRectMake(800.0f, 30.0f, self.frame.size.width, 60.0f)] autorelease];
		singleCountLabel.font = [UIFont systemFontOfSize:50.0f];
		singleCountLabel.backgroundColor = [UIColor clearColor];
		singleCountLabel.text = [NSString stringWithFormat:@" * %d", singleCount];
		[self addSubview:singleCountLabel];
		
		UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
		[clearBtn setTitle:@"清空" forState:UIControlStateNormal];
		clearBtn.titleLabel.font = [UIFont systemFontOfSize:30.0f];
		clearBtn.frame = CGRectMake(900.0f, 10.0f, 100.0f, 100.0f);
		[clearBtn addTarget:self action:@selector(clear:) forControlEvents:UIControlEventTouchUpInside];
		[self addSubview:clearBtn];
    }
    return self;
}

- (void)packageAdd:(id)sender{
	packageCount++;
	packageCountLabel.text = [NSString stringWithFormat:@" * %d", packageCount];
	
	if (orderDelegate && [orderDelegate respondsToSelector:@selector(OrderCellDelegate:packageCountDidChangeTo:inRow:)]) {
		[orderDelegate OrderCellDelegate:self packageCountDidChangeTo:packageCount inRow:row];
	}
}

- (void)singleAdd:(id)sender{
	singleCount++;
	
	singleCountLabel.text = [NSString stringWithFormat:@" * %d", singleCount];
	
	if (orderDelegate && [orderDelegate respondsToSelector:@selector(OrderCellDelegate:singleCountDidChangeTo:inRow:)]) {
		[orderDelegate OrderCellDelegate:self singleCountDidChangeTo:singleCount inRow:row];
	}
}

- (void)clear:(id)sender{
	packageCount = 0;
	singleCount = 0;
	
	packageCountLabel.text = [NSString stringWithFormat:@" * %d", packageCount];
	singleCountLabel.text = [NSString stringWithFormat:@" * %d", singleCount];
	
	if (orderDelegate && [orderDelegate respondsToSelector:@selector(OrderCellDelegate:packageCountDidChangeTo:inRow:)]) {
		[orderDelegate OrderCellDelegate:self packageCountDidChangeTo:packageCount inRow:row];
	}
	if (orderDelegate && [orderDelegate respondsToSelector:@selector(OrderCellDelegate:singleCountDidChangeTo:inRow:)]) {
		[orderDelegate OrderCellDelegate:self singleCountDidChangeTo:singleCount inRow:row];
	}
}

- (void)refreshWithName:(NSString *)newName andPackageCount:(NSInteger)newPackageCount andSingleCount:(NSInteger)newSingleCount{
	name.text = newName;
	
	packageCount = newPackageCount;
	singleCount = newSingleCount;
	
	packageCountLabel.text = [NSString stringWithFormat:@" * %d", packageCount];
	singleCountLabel.text = [NSString stringWithFormat:@" * %d", singleCount];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    
    //[super setSelected:selected animated:animated];
    
    // Configure the view for the selected state.
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
	
	//[super setHighlighted:highlighted animated:animated];
    
    // Configure the view for the highlighted state.
}


- (void)dealloc {
    [super dealloc];
}


@end
